<?php 

class SectionHelper extends AppHelper
{
	var $helpers = array('Html','Javascript','Effects.Thickbox');

	var $systemModules = array();
	
	function beforeRender()
	{
		if($this->isModuleEditorMode())
		{
			$this->addJUI();
			$this->getView()->addScript('giga_ajax_lib',$this->Javascript->link('/effects/js/jquery.form.js',true));
			$this->getView()->addScript("ajax_form",$this->Javascript->link('/cms/js/giga_ajax_lib.js',true));
			$this->getView()->addScript("module_editor",$this->Javascript->link('/modules/js/editor.js',true));
			$this->getView()->addScript("module_editor_css",$this->Html->css('/modules/css/module.css'));
		}
	}

	function appendSystemModule($section, $module)
	{
		if(!isset($this->systemModules[$section]))
		{
			$this->systemModules[$section] = array();
		}
		// to-do maybe
		$defaults = array(
			'id' => mt_rand(),
			'cache' => '',
			'show_title' => false,
			'params' => array(),
			'apply_default_class' => true
		);
		$module = am($defaults, $module);
		$this->systemModules[$section][] = array('Module'=>$module);
	}

	function render($section)
	{
		// find if there are system-defined modules for this section
		$systemModules = array();
		if(isset($this->systemModules[$section]) && !empty($this->systemModules[$section]))
		{
			$systemModules = $this->systemModules[$section];
		}

		$moduleModel = cri('Modules.Module');
		$userModules = $moduleModel->getSectionModules($section);
		
		$modules = array_merge($systemModules, $userModules);

		$content = $cacheKey = null;
		$contents = array(); 
		foreach($modules as $module)
		{	
			$suffix = "";
			$moduleContent = null;
			//@todo this is a review specific work around for dynamic
			//this code should be in app, so that this helper can be used generically but due 
			//time contraint,  a quick fix is being added.It should get removed from here.
			if(cr('DynamicPage.field'))
			{
				$suffix = cr('DynamicPage.field').'_'.cr('DynamicPage.value').'_';
			}

			$cacheKey = $module['Module']['name'].'_'.'Module_'.$suffix.$module['Module']['id'];
			$cacheKey = low($cacheKey)."_".cr('Config.language');
			$cacheTime = $module['Module']['cache'];
				
			GigaCache::set(array('path'=>$moduleModel->getModuleCahcePath(),'duration'=>"+2 minutes"));
			$cache = false;		
			
			if($cacheTime != '0' && $cacheTime != '')
			{		
				$cache = true;		
			}
			
			if($cache == true)
			{		
				$moduleContent = GigaCache::read($cacheKey);											
			}	
			//force direct read if editor mode.
			if($this->isModuleEditorMode() == true)
			{
				$moduleContent = null;
				$cache = false;
			}
			if(empty($moduleContent))
			{	
				$moduleContent = $moduleModel->getModuleObject($module['Module']['name'])->render($module['Module']['params']);
				if($moduleContent!==false)
				{
					$moduleContent = $this->wrap($module['Module'], $moduleContent);
				}
				if($cache == true)
				{
					GigaCache::set(array('path'=>$moduleModel->getModuleCahcePath(),'duration'=>"+2 minutes"));
					GigaCache::write($cacheKey,$moduleContent);
				}			
							
			}
			$content  .= $moduleContent;
						
		}
		if($this->isModuleEditorMode())
		{
			$sectionId = "module_section_".$section;
			$content = "<div class=\"module_section\"  id=\"$sectionId\">$content</div>";
		}
		return $content;
	}

	/**
	 * Wrapper for module
	 *
	 * @param unknown_type $title
	 * @param unknown_type $content
	 * @return unknown
	 */
	function wrap($module, $content)
	{
		// check theme block formatter
		$output = "";
		if (file_exists(APP . 'views' . DS . $this->getView()->themePath . 'module.php')) 
		{
			require_once (APP . 'views' . DS . $this->getView()->themePath . 'module.php');
			if ($module['style'] && function_exists("module_" . $module['style'])) 
			{
				$fn = "module_" . $module['style'];
				$output = $fn($module, $content);
			} 
			else 
			{
				$output = module_default($module, $content);
			}
		} 
		else 
		{
			$output = $this->_defaultStyle($module, $content);
		}
		if($this->isModuleEditorMode())
		{
			$moduleDomId = 'module_'.$module['id'];
			$output  = '<div id="'.$moduleDomId.'" class="module_sort_block">'.$output.'</div>';
		}
		return $output ;		
	}


	function _defaultStyle($module, $content) 
	{
		$moduleWrapperclass = 'module';
		if(!isset($module['apply_default_class']) || $module['apply_default_class'] != true)
		{
			$moduleWrapperclass = '';
		}
		if(!empty($module['style']))
		{
			$moduleWrapperclass .= ' '.$module['style'];
		}
		$output = '<div class="'.trim($moduleWrapperclass).'">';
		if($module['show_title'] == 1)
		{
			$output .= '<h2 class="module_title">' . __($module['title']) . '</h2>';
//			$output .= '<h4 class="module_title">' . __($module['title']) . '</h4>';
		}
		$output .=  '<div class="module_content">' . $content . "</div></div>";
		return $output;
	}

	function isModuleEditorMode()
	{
		$return = false;
		if(cr('Modules.moduleEditorMode') == true)
		{
			$return = true;
		}
		return $return;
	}
}

?>